1   // Copyright 2013 The Apache Software Foundation
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  package org.apache.tapestry5.ioc.internal;
15  
16  import java.lang.annotation.Annotation;
17  import java.lang.reflect.Method;
18  
19  import org.apache.tapestry5.ioc.annotations.Advise;
20  import org.apache.tapestry5.ioc.annotations.IntermediateType;
21  import org.apache.tapestry5.plastic.MethodAdvice;
22  import org.apache.tapestry5.plastic.MethodInvocation;
23  
24  final public class TestAdvice implements MethodAdvice {
25  
26      public static final String ANNOTATION_FOUND = "Annotation found!";
27  
28      @Override
29      public void advise(MethodInvocation invocation) {
30  
31          final Method method = invocation.getMethod();
32          boolean annotationFoundInMethod = checkAnnotation(method.getAnnotation(Advise.class));
33          boolean annotationFoundThroughAnnotationProvider = checkAnnotation(invocation.getAnnotation(Advise.class));
34          IntermediateType parameterAnnotation = null;
35          final Annotation[][] parameterAnnotations = method.getParameterAnnotations();
36          if (parameterAnnotations.length > 0 && parameterAnnotations[0].length > 0) {
37              parameterAnnotation = (IntermediateType) parameterAnnotations[0][0];
38          }
39          boolean annotationParameter = parameterAnnotation != null && parameterAnnotation.value() == String.class;
40          
41          if (annotationFoundInMethod && annotationFoundThroughAnnotationProvider && annotationParameter) 
42          {
43              invocation.setReturnValue(ANNOTATION_FOUND);
44          }
45          else {
46              invocation.proceed();
47          }
48          
49      }
50  
51      private boolean checkAnnotation(Advise annotation)
52      {
53          return annotation != null && "id".equals(annotation.id()) && NonAnnotatedServiceInterface.class.equals(annotation.serviceInterface());
54      }
55      
56  }